<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Application_Model_EventModel {

    private $filePath = '/xml/ev.xml';

    public function getNewId($liste) {
        $v1 = 0;
        $v2 = 0;
        foreach ($liste as $l) {
            if ($l->hasAttribute("id")) {
                $v2 = $l->getAttribute('id');
                if ($v2 > $v1) {
                    $v1 = $v2;
                }
            }
        }
        return $v1 + 1;
    }

    public function readAll() {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//event');
        return $this->getEvents($dom, $listDomElements);
    }

    public function countAll() {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//event');
        $nb = count($listDomElements);

        return $nb;

        $this->view->xml = $listDomElements;
        $this->view->xml_nb = $nb;
    }

    public function read($id) {
        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $event = new Application_Model_Event();

        if ($dom->queryXpath('//event[@id=' . $id . ']') == null) {
            return null;
        }

        $event->setId($id);
        $event->setDate($dom->queryXpath('//event[@id=' . $id . ']/date')->current()->textContent);
        $event->setContent($dom->queryXpath('//event[@id=' . $id . ']/content')->current()->textContent);
        $event->setOwner(new Application_Model_People(
                        $dom->queryXpath('//event[@id=' . $id . ']/owner/name')->current()->textContent,
                        $dom->queryXpath('//event[@id=' . $id . ']/owner/firstName')->current()->textContent
        ));

        $event->setPicturepath($dom->queryXpath('//event[@id=' . $id . ']/picture/@url')->current()->textContent);
        $event->setTitle($dom->queryXpath('//event[@id=' . $id . ']/title')->current()->textContent);
        $comments = Array();
        foreach ($dom->queryXpath('//event[@id=' . $id . ']/comment') as $comment) {
            $com = new Application_Model_Comment();

            $com->setContent($comment->getElementsByTagName('content')->item(0)->textContent);
            $com->setDate($comment->getElementsByTagName('date')->item(0)->textContent);
            $name = $comment->getElementsByTagName('name')->item(0)->textContent;
            $firstName = $comment->getElementsByTagName('firstName')->item(0)->textContent;
            $author = new Application_Model_People($name, $firstName);
            $com->setAuthor($author);
            $com->setId($comment->getAttribute('id'));
            $comments[] = $com;
        }
        $event->setComments($comments);
        return $event;
    }

    public function insert($event) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $allEvents = $dom->getElementsByTagName('events');
        $id = $event->getId();
        if (!isset($id) || $id == null || $id == '') {
            $id = $this->getNewId($dom->getElementsByTagName('event'));
        }
        $root = $dom->createElement('event');
        $root->setAttribute('id', $id);
        $dom->appendChild($root);
        $date = $dom->createElement('date');
        $elemtext = $dom->createTextNode($event->getDate());
        $date->appendChild($elemtext);
        $root->appendChild($date);
        $title = $dom->createElement('title');
        $elemtext = $dom->createTextNode($event->getTitle());
        $title->appendChild($elemtext);
        $root->appendChild($title);
        $owner = $dom->createElement('owner');
        $root->appendChild($owner);
        $own_name = $dom->createElement("name");
        $elemtext = $dom->createTextNode($event->getOwner()->getName());
        $own_name->appendChild($elemtext);
        $own_firstname = $dom->createElement("firstName");
        $elemtext = $dom->createTextNode($event->getOwner()->getFirstName());
        $own_firstname->appendChild($elemtext);
        $owner->appendChild($own_name);
        $owner->appendChild($own_firstname);
        $picture = $dom->createElement("picture");
        $picture->setAttribute('url', $event->getPicturepath());
        $root->appendChild($picture);
        $content = $dom->createElement("content");
        $elemtext = $dom->createTextNode($event->getContent());
        $content->appendChild($elemtext);
        $root->appendChild($content);

        $events = $allEvents->item(0);
        $events->appendChild($root);


        // $this->setElement($dom, $dom->root, $root);
        // Mise à jour du fichier source original
        $dom->save(APPLICATION_PATH . $this->filePath);
        //  echo APPLICATION_PATH . '/xml/ev.xml';
    }

    function delete($id) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $root = $dom->getElementsByTagName('events')->item(0);
        $allEvents = $dom->getElementsByTagName('event');
        foreach ($allEvents as $event) {
            if ($event->hasAttribute("id")) {
                $v2 = $event->getAttribute('id');
                if ($v2 == $id) {
                    $root->removeChild($event);
                    $dom->save(APPLICATION_PATH . $this->filePath);
                    return true;
                }
            }
        }
        return false;
    }

    public function edit($id, $event) {
        if ($this->delete($id)) {
            $event->setId($id);
            $this->insert($event);
        }
    }

    public function addComment($id, $comment) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $allEvents = $dom->getElementsByTagName('event');
        $id = $comment->getId();
        if (!isset($id) || $id == null || $id == '') {
            $id = $this->getNewId($dom->getElementsByTagName('comment'));
        }
        $root = $dom->createElement('comment');
        $root->setAttribute('id', $id);
        $dom->appendChild($root);
        $date = $dom->createElement('date');
        $elemtext = $dom->createTextNode($comment->getDate());
        $date->appendChild($elemtext);
        $root->appendChild($date);
        $author_name = $dom->createElement("name");
        $elemtext = $dom->createTextNode($comment->getAuthor()->getName());
        $author_name->appendChild($elemtext);
        $root->appendChild($author_name);
        $author_firstname = $dom->createElement("firstName");
        $elemtext = $dom->createTextNode($comment->getAuthor()->getFirstName());
        $author_firstname->appendChild($elemtext);
        $root->appendChild($author_firstname);
        $content = $dom->createElement("content");
        $elemtext = $dom->createTextNode($comment->getContent());
        $content->appendChild($elemtext);
        $root->appendChild($content);

        $comments = $allEvents->item(0);
        $comments->appendChild($root);


        // $this->setElement($dom, $dom->root, $root);
        // Mise à jour du fichier source original
        $dom->save(APPLICATION_PATH . $this->filePath);
        //  echo APPLICATION_PATH . '/xml/ev.xml';
    }

    public function removeComment($id) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $allComments = $dom->getElementsByTagName('comment');
        foreach ($allComments as $comment) {
            if ($comment->hasAttribute("id")) {
                $v2 = $comment->getAttribute('id');
                if ($v2 == $id) {
                    $root = $comment->parentNode;
                    $root->removeChild($comment);
                    $dom->save(APPLICATION_PATH . $this->filePath);
                    return true;
                }
            }
        }
        return false;
    }

    public function addEvents($tabEvent1, $tabEvent2) {
        $found = false;
        foreach ($tabEvent2 as $event2) {
            foreach ($tabEvent1 as $event1) {
                if ($event2->getId() == $event1->getId()) {
                    $event1->increasePertinence();
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                $tabEvent1[] = $event2;
            }
            $found = false;
        }
        return $tabEvent1;
    }

    public function search($key) {
        
        $tabEvent = $this->searchByTitle($key);
        $tabContent = $this->searchByContent($key);
        $tabEvent = $this->addEvents($tabEvent, $tabContent);
        $tabAuthor = $this->searchByAuthor($key);
        $tabEvent = $this->addEvents($tabEvent, $tabAuthor);
        $tabDate = $this->searchByDate($key);
        $tabEvent = $this->addEvents($tabEvent, $tabDate);
        usort($tabEvent, function( Application_Model_Event $a, Application_Model_Event $b) {
                    $a = $a->getPertinence();
                    $b = $b->getPertinence();
                    if ($a == $b) {
                        return 0;
                    }
                    return ($a < $b) ? -1 : 1;
                });
        return $tabEvent;
    }

    public function searchByTitle($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//event[./title[contains(.,\'' . $key . '\')]]');
        return $this->getEvents($dom, $listDomElements);
    }

    public function searchByContent($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//event[./content[contains(.,\'' . $key . '\')]]');
        return $this->getEvents($dom, $listDomElements);
    }

    public function searchByAuthor($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//event[./owner/name[contains(.,\'' . $key . '\')] or ./owner/firstname[contains(.,\'' . $key . '\')]]');
        return $this->getEvents($dom, $listDomElements);
    }

    public function searchByDate($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//event[./date[contains(.,\'' . $key . '\')]]');
        return $this->getEvents($dom, $listDomElements);
    }

    public function getEvents($dom, $listDomElements) {

        $tabEvent = Array();
        foreach ($listDomElements as $node) {
            $event = new Application_Model_Event();
            $event->setId($node->getAttribute('id'));

            $event->setDate($dom->queryXpath('//event[@id=' . $event->getId() . ']/date')->current()->textContent);
            $event->setContent($dom->queryXpath('//event[@id=' . $event->getId() . ']/content')->current()->textContent);
            $event->setOwner(new Application_Model_People($dom->queryXpath('//event[@id=' . $event->getId() . ']/owner/name')->current()->textContent, $dom->queryXpath('//event[@id=' . $event->getId() . ']/owner/firstName')->current()->textContent));

            $event->setPicturepath($dom->queryXpath('//event[@id=' . $event->getId() . ']/picture/@url')->current()->textContent);
            $event->setTitle($dom->queryXpath('//event[@id=' . $event->getId() . ']/title')->current()->textContent);
            $tabEvent[] = $event;
        }

        return $tabEvent;
    }

}

?>
